package com.yic.module.wms.controller.admin.itemwarehouseinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.wms.controller.admin.itemwarehouseinfo.vo.*;
import com.yic.module.wms.dal.dataobject.itemwarehouseinfo.ItemWarehouseInfoDO;
import com.yic.module.wms.convert.itemwarehouseinfo.ItemWarehouseInfoConvert;
import com.yic.module.wms.service.itemwarehouseinfo.ItemWarehouseInfoService;

@Tag(name = "管理后台 - 库存明细")
@RestController
@RequestMapping("/wms/item-warehouse-info")
@Validated
public class ItemWarehouseInfoController {

    @Resource
    private ItemWarehouseInfoService itemWarehouseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建库存明细")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse-info:create')")
    public CommonResult<Long> createItemWarehouseInfo(@Valid @RequestBody ItemWarehouseInfoCreateReqVO createReqVO) {
        return success(itemWarehouseInfoService.createItemWarehouseInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存明细")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse-info:update')")
    public CommonResult<Boolean> updateItemWarehouseInfo(@Valid @RequestBody ItemWarehouseInfoUpdateReqVO updateReqVO) {
        itemWarehouseInfoService.updateItemWarehouseInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse-info:delete')")
    public CommonResult<Boolean> deleteItemWarehouseInfo(@RequestParam("id") Long id) {
        itemWarehouseInfoService.deleteItemWarehouseInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse-info:query')")
    public CommonResult<ItemWarehouseInfoRespVO> getItemWarehouseInfo(@RequestParam("id") Long id) {
        ItemWarehouseInfoDO itemWarehouseInfo = itemWarehouseInfoService.getItemWarehouseInfo(id);
        return success(ItemWarehouseInfoConvert.INSTANCE.convert(itemWarehouseInfo));
    }

    @GetMapping("/list")
    @Operation(summary = "获得库存明细列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse-info:query')")
    public CommonResult<List<ItemWarehouseInfoRespVO>> getItemWarehouseInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<ItemWarehouseInfoDO> list = itemWarehouseInfoService.getItemWarehouseInfoList(ids);
        return success(ItemWarehouseInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存明细分页")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse-info:query')")
    public CommonResult<PageResult<ItemWarehouseInfoRespVO>> getItemWarehouseInfoPage(@Valid ItemWarehouseInfoPageReqVO pageVO) {
        PageResult<ItemWarehouseInfoDO> pageResult = itemWarehouseInfoService.getItemWarehouseInfoPage(pageVO);
        return success(ItemWarehouseInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存明细 Excel")
    @PreAuthorize("@ss.hasPermission('wms:item-warehouse-info:export')")
    @OperateLog(type = EXPORT)
    public void exportItemWarehouseInfoExcel(@Valid ItemWarehouseInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ItemWarehouseInfoDO> list = itemWarehouseInfoService.getItemWarehouseInfoList(exportReqVO);
        // 导出 Excel
        List<ItemWarehouseInfoExcelVO> datas = ItemWarehouseInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "库存明细.xls", "数据", ItemWarehouseInfoExcelVO.class, datas);
    }

}
